package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;

public class ChinareportsNewsAnalyse implements AnalyseNews{
/**
 * http://www.chinareports.org.cn/news-13-18335.html
 * http://www.chinareports.org.cn/news-12-18290.html
 * http://www.chinareports.org.cn/news-13-18314.html
 * http://www.chinareports.org.cn/news-1510-1433.html
 * http://www.chinareports.org.cn/zz/gdft/news210376.htm
 */
	private static final String[] regex={
			"http://www.chinareports.org.cn/news-[0-9]*-[0-9]*.html",
			"http://www.chinareports.org.cn/.*news[0-9]*.htm"
		};
			
			public boolean isDetailPage(String url) {
				for (int i=0; i < regex.length; i++) {
					if (url.matches(regex[i])) {
						return true;
					}
				}
				return false;
			}

			
			public NewsMeta Update(NewsMeta meta) {
				return null;
			}



			
			public NewsMeta parserHtml(UrlMeta urlMeta) {
				NewsMeta news=new NewsMeta();
				if (urlMeta.getHtml() == null) {
				}
				String htmltxt=urlMeta.getHtml();
				String url=urlMeta.getUrl();


				String title=null;
				String content=null;
				String author=null;
				Long date=null;
				Document doc=Jsoup.parse(htmltxt);
				/**
				 * 标题
				 */
				title=doc.select("h3.h3_title").text();
				if(StringUtils.isBlank(title)){
					title=doc.select("h2.new_title").text();
					if(StringUtils.isBlank(title)){
						title=doc.select("div.new_title_new").text();
						if(StringUtils.isBlank(title)){
							title=doc.select("div.title_t").text();
						}
					}
				}
				/**
				 * 内容
				 */
				content=HtmlCleaner.getContentHtml(url ,doc.select("div.news"));
				if(StringUtils.isBlank(content)){
					content=HtmlCleaner.getContentHtml(url ,doc.select("div.new_edit"));
				}
				if(StringUtils.isBlank(content)){
					content=HtmlCleaner.getContentHtml(url ,doc.select("div.newlist"));
				}
				if(StringUtils.isBlank(content)){
					content=HtmlCleaner.getContentHtml(url ,doc.select("div.info_txt"));
				}
				/**
				 * 日期
				 */
				date=DateUtils.matchDate(doc.select("p.news_data").text());
				if(date == null){
					date=DateUtils.matchDate(doc.select("div.new_top b").text());
					if(date == null){
						date=DateUtils.matchDate(doc.select("div.num").text());
						if(date == null){
							date=DateUtils.matchDate(doc.select("div.info_txt").text());
						}
					}
				}
				author=JSoupUtils.matchAuthor(doc, "来源：");

				news.setUrl(url);
				news.setAuthor(author);
				news.setTitle(title);
				news.setContent(content);
				news.setDate(date);
				return news;
			}

			public static void main(String[] args) {
				ChinareportsNewsAnalyse test=new ChinareportsNewsAnalyse();

				String url="http://www.chinareports.org.cn/news-1831-241.html";
				System.out.println(test.isDetailPage(url));
				UrlMeta meta=CrawlHTML.responseToURL(url);
				NewsMeta parserHtml=test.parserHtml(meta);
				System.out.println(parserHtml);
			}
	
	public boolean isNeedUpdate(){
		return false;
	}
}
